#!/bin/zsh

#  add_prefix.sh
#  Example
#
#  Created by 晓伟王 on 2019/2/26.
#  Copyright © 2019 Zhang. All rights reserved.
#
#将该文件拖拽到要混淆的工程文件下
#终端中cd到该文件所在的上级目录下
#调用sh add_prefix.sh
#因存在正则匹配单边词缀无法识别问题 会出现重复添加前缀问题  要手动修改
#

#/Users/xiaoweiwang/Downloads/iOSConfuse-master/Example
#export PATH=/bin:/usr/bin:/usr/local/bin:"${PATH}"
#export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11R6/bin
#export PATH=/Users/cygr-0101-01-0133/Documents/project/flutter/bin:$PATH
CONFUSE_PREFIX="class"
CONFUSE_PREFIXs="WXW" #词缀
OLD_CONFUSE_PREFIX="WX"

# 认为定义了‘PROJECT_NAME’的是从Xcode运行，未定义则是从终端运行
if [ -z "$PROJECT_NAME" ]; then
CONFUSE_DIR="."
else
CONFUSE_DIR="${SRCROOT}/${PROJECT_NAME}"
fi

BACKUP_FILE=".backup.log"
SOURCE_ARRAY=( "*.swift")
class_Array=("AppDelegate")
target_array=()

searchClassName() {
    FILE_DIR=$1
    echo "m路径为：$FILE_DIR"
    echo "参数个数：$#  参数值为:$*"
    IFS=','
#    error_file=$*
#    FILE_DIR=${error_file//\\/n}
#    echo "修改过后路径为 $FILE_DIR"
cat  $FILE_DIR | egrep -n -w 'class|struct|enum' | grep -v -w '@class\|func\|let\|var' | grep -v -w "\"class\""| while read  line
        do
        echo $line

        result=$(echo $line | grep "\<${CONFUSE_PREFIXs}")
        if [ "$result" != "" -a "$result" != "$OLD_CONFUSE_PREFIX" ]
        then
            echo "舍弃"
            continue
        fi
        echo "定位"
        lineNum=${line%%:*}
        keyWord="class"
        if [[ $line == *'class'* ]]
        then
            echo "包含class"
            keyWord="class"
        elif [[ $line == *'struct'* ]]
        then
            echo "包含struct"
            keyWord="struct"
        elif [[ $line == *'enum'* ]]
        then
            echo "包含enum"
            keyWord="enum"
        fi
        echo "关键字为 $keyWord"

        targetStr=${line#*$keyWord}
        targetStr=${targetStr#* }
        targetStr=${targetStr%%:*}
        targetStr=${targetStr%% *}
        targetStr=${targetStr%%<*}
#        if [ $targetStr = "\"class\""]
        echo "匹配目标字符串为 $targetStr"
        if [ -z $targetStr ]
        then
            echo "放弃0"
        continue
        fi
        if [[ $targetStr == *" "* ]]
        then
            continue
        fi
        if [[ $targetStr == *"{"* ]]
        then
            targetStr=${targetStr%%{*}
        fi
        echo "匹配目标字符串为：$targetStr"

#        exchangeStr=$CONFUSE_PREFIXs$targetStr
#        echo $targetStr
#        echo $exchangeStr
        echo $targetStr >>$BACKUP_FILE
#        sed -i "" "${lineNum}s/${targetStr}/$exchangeStr/" $FILE_DIR
    done

}

exchangeKeyWord(){

#    key_words=$2
    IFS=','
    FILE_NAME=$1
    i=0

#    while [ $i -lt ${#key_word_arr[@]} ]; do
    cat $BACKUP_FILE | sort | uniq | while read -ra key_word; do

#        echo ${key_word_arr[i]}
        echo "key开始"
#        key_word=${key_word_arr[i]}
        echo "\"$key_word\""
        exchange_str=$CONFUSE_PREFIXs$key_word
        #判断是否含有旧前缀

        if [[ "$key_word" =~ ^$OLD_CONFUSE_PREFIX.* ]]; then
            exchange_str=${key_word/$OLD_CONFUSE_PREFIX/$CONFUSE_PREFIXs}
            echo "替换串更新"
        fi
        cat  $FILE_NAME | grep -n -w $key_word | while read  line
        do
            lineNum=${line%%:*}
            #参数分隔符莫名混乱这里重置一下，防止FILE_NAME传参错误,影响匹配替换
            IFS=','
            echo "原关键词为 : $key_word"
            echo "替换串为 : $exchange_str"
            echo "$key_word 替换为 $exchange_str"
            sed -i "" "${lineNum}s~$key_word~$exchange_str~g" $FILE_NAME

        done
        let i++
echo "key结束"
    done

}



findSwiftFile() {

    rm -if $BACKUP_FILE
    touch $BACKUP_FILE
    NAMES="-name \"${SOURCE_ARRAY[0]}\""
    i=1
        while [ $i -lt ${#SOURCE_ARRAY[@]} ]; do
        NAMES+=" -or -name \"${SOURCE_ARRAY[$i]}\""
        let i++
    done


    eval "find $CONFUSE_DIR $NAMES" | while read file;
    do
        filename=${file%.*}
        filename=$filename".xib"
        if [ -e $filename ]
        then
            echo "存在xib"
        else
            echo "文件名为 $file"
            searchClassName "$file"

        fi
    done


    eval "find $CONFUSE_DIR $NAMES" | while read file;
    do
    #echo "文件名为 ->$file "
        exchangeKeyWord "$file"
    #echo "123"
    done

}


findSwiftFile


#另一种不完善的匹配替换方法
zancun() {
#echo "行开始"
##            echo $line
#
##            echo "开始"
##            echo $exchange_str
##echo "结束"
#            IFS=' ' arr=($line)
#            si=0
#            for x in ${arr[@]}; do
#                result=$(echo $x | grep -w "${key_word}" |  grep -v -w "["]class["]")
#                if [[ "$result" != "" ]]
#                then
#                    echo "匹配成功"
#                    echo "原子串为 ： $x"
#                    echo "原关键词为 : $key_word"
#                    echo "替换串为 : $exchange_str"
#                    xx=${x/$key_word/$exchange_str}
#                    echo $xx
#                    arr[si]=$xx
#                    echo "结束"
#                else
#                    echo "放弃 $x"
#                fi
#                let si++
#            done
##            echo ${arr[@]}
#            niustr=''
#            for x in ${arr[@]}; do
#                niustr=$niustr$x" "
#            done
#            niustr=${niustr#*:}
###            echo $niustr
#            niuline=${line#*:}
#            lineNum=${line%%:*}
##            exchangeStr=$CONFUSE_PREFIXs$key_word
##            echo $key_word
##            echo $exchangeStr
#echo "替换 $niuline 为 $niustr"
#echo "替换文件为 $FILE_NAME"
#            IFS=','
#
#            sed -i "" "${lineNum}s~$niuline~$niustr~g" $FILE_NAME
##echo "$key_word 替换为 $exchange_str"
##            sed -i "" "${lineNum}s/$key_word/$exchange_str/g" $FILE_NAME
#echo "行结束"
    echo " "
}
